package com.zzyl.generator.Test;

import com.zzyl.common.constant.Constants;
import org.apache.logging.log4j.spi.ThreadContextMapFactory;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class Demo {
    public static void main(String[] args) throws IOException {
        //初始化模板引擎
      Properties p = new Properties();
        //表示告诉我们去哪一个类下找对应的模版资源文件
        p.setProperty("resource.loader.file.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        // 定义字符集
        p.setProperty(Velocity.INPUT_ENCODING, Constants.UTF8);
        Velocity.init(p);
        //获取对应的模板文件
        Template template = Velocity.getTemplate("vms/index.html.vm", Constants.UTF8);
        //输出指定位置
        FileWriter fileWriter = new FileWriter("zzyl-generator\\\\\\\\src\\\\\\\\main\\\\\\\\resources\\\\\\\\index.html");
        /**
         *  4.合并模版和数据
         *  context:数据
         *  template:模版
         *  fileWriter:输出位置
         */
        Context context=new VelocityContext();
        context.put("message","王卓豪");
        template.merge(context,fileWriter);

        //5.关闭流
        fileWriter.close();
    }
    }
